package com.yuandengta.yomall.product.web;

import com.yuandengta.yomall.product.entity.CategoryEntity;
import com.yuandengta.yomall.product.service.CategoryService;
import com.yuandengta.yomall.product.vo.Catalog2VO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 商城首页 {@link IndexController}
 *
 * @author Kevin
 * @email: 178676392@qq.com
 */
@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 一级分类
     * @return
     */
    @GetMapping({"/", "/index.html"})
    public String index(Model model) {
        // 查询所有的一级分类
        List<CategoryEntity> categories = categoryService.getLevel1Categories();
        model.addAttribute("categories", categories);
        return "index";
    }

    /**
     * 一次性组装分类数据
     * @return
     */
    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catalog2VO>> getCatalogJson() {
        Map<String, List<Catalog2VO>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }

    /**
     * 测试简单服务
     * @return
     */
    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}
